{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "localtext = open('10.html', 'r', encoding='utf8')\n",
    "sp = BeautifulSoup(localtext, 'lxml')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n\\n\\n\\n\\ntext和string\\n\\n\\n\\np1\\np2\\np3\\n\\n            cp1\\n            s1\\n            cp2\\n            s2\\n            cp3\\n        \\na1\\na2\\na3\\n\\n\\n'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<p>p1</p>\n",
      "p1\n"
     ]
    }
   ],
   "source": [
    "print(sp.p)\n",
    "print(sp.p.string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<p id=\"cp\">\n",
      "            cp1\n",
      "            <span>s1</span>\n",
      "            cp2\n",
      "            <span>s2</span>\n",
      "            cp3\n",
      "        </p>\n"
     ]
    }
   ],
   "source": [
    "tp = sp.find('p', {'id':'cp'})\n",
    "print(tp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None --------------\n"
     ]
    }
   ],
   "source": [
    "print(tp.string, '--------------')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object Tag._all_strings at 0x000000000881C0C8>\n"
     ]
    }
   ],
   "source": [
    "## 生成器\n",
    "print(tp.strings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n            cp1\\n            ', 's1', '\\n            cp2\\n            ', 's2', '\\n            cp3\\n        ']\n"
     ]
    }
   ],
   "source": [
    "print(list(tp.strings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<p>p1</p>\n",
      "p1\n",
      "['p1']\n"
     ]
    }
   ],
   "source": [
    "print(sp.p)\n",
    "print(sp.p.string)\n",
    "print(list(sp.p.strings))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## stripped_strings 分割字符，在strings的基础之上，做了一个不可见字符的剔除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n            cp1\\n            ', 's1', '\\n            cp2\\n            ', 's2', '\\n            cp3\\n        ']\n",
      "['cp1', 's1', 'cp2', 's2', 'cp3']\n"
     ]
    }
   ],
   "source": [
    "print(list(tp.strings))\n",
    "## 将不可见字符【\\n 换行和空格】全部删除了\n",
    "print(list(tp.stripped_strings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n\\n\\n\\n\\ntext和string\\n\\n\\n\\np1\\np2\\np3\\n\\n            cp1\\n            s1\\n            cp2\\n            s2\\n            cp3\\n        \\na1\\na2\\na3\\n\\n\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n\\n\\n\\n\\ntext和string\\n\\n\\n\\np1\\np2\\np3\\n\\n            cp1\\n            s1\\n            cp2\\n            s2\\n            cp3\\n        \\na1\\na2\\na3\\n\\n\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 原生的含不可见字符串的子孙级标签全部文本字符串【通过strings合成】\n",
    "''.join(list(sp.strings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'text和stringp1p2p3cp1s1cp2s2cp3a1a2a3'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 非原生的不含不可见字符串的子孙级标签全部文本字符串【通过stripped_strings合成】\n",
    "''.join(list(sp.stripped_strings))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
